package com.baibei.pay.recharge;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.baibei.ebec.sdk.TokenInfo;
import com.baibei.model.UserInfo;
import com.baibei.module.AppRouter;
import com.baibei.pay.R;
import com.baibei.pay.R2;
import com.blankj.utilcode.utils.ToastUtils;
import com.rae.swift.session.SessionManager;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HXPayWebViewActivity extends AppCompatActivity {

    @BindView(R2.id.wv)
    WebView wv;
    @BindView(R2.id.pb)
    ProgressBar pb;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    private String url;
    private Map<String, String> map;
    private String title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hxpay_webview);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        title = intent.getStringExtra("title");
        tvTitle.setText(title);
        init();
    }

    private void init() {
        Map<String, String> map = new HashMap<>();
        map.put("Referer", "http://h5.shzstr.cn");
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);

        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setSupportZoom(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setGeolocationEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setAllowFileAccess(true);
        settings.setSupportZoom(true);
        settings.setLoadWithOverviewMode(true);
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存内容

        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.contains("100bei")){
                    ToastUtils.showShortToast("充值成功");
                    finish();
                    return false;
                }
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }

            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (request.getUrl().toString().contains("100bei")){
                    ToastUtils.showShortToast("充值成功");
                    finish();
                    return false;
                }
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
//        wv.setWebChromeClient(new WebChromeClient() {
//            @Override
//            public void onProgressChanged(WebView view, int newProgress) {
//                super.onProgressChanged(view, newProgress);
//                if (newProgress == 100) {
//                    pb.setVisibility(View.GONE);//加载完网页进度条消失
//                } else {
//                    pb.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
//                    pb.setProgress(newProgress);//设置进度值
//                }
//            }
//        });
//        wv.addJavascriptInterface(new JiaoHu(), "android");
        wv.loadUrl(url);
    }

    @OnClick(R2.id.iv_back)
    public void onFinishClicked() {
        finish();
    }

    public class JiaoHu {
        @JavascriptInterface
        public void backHome() {
            AppRouter.routeToMain(getApplicationContext(), 0);
        }

        @JavascriptInterface
        public void toShare() {
            AppRouter.routeToMain(getApplicationContext(), "share");
        }

        /**
         * 获取用户信息
         */

        @JavascriptInterface
        public String getUserInfo() {
            TokenInfo userToken = SessionManager.getDefault().getUserToken();
            String accessToken = userToken.getAccessToken();
            return accessToken;
        }

        @JavascriptInterface
        public int getUserId() {
            UserInfo userInfo = SessionManager.getDefault().getUser();
            return userInfo.getUserId();
        }

        @JavascriptInterface
        public void toQuan() {
            AppRouter.routeToTicket(getApplicationContext());
        }

        @JavascriptInterface
        public void toLogin() {
            AppRouter.routeToLogin(HXPayWebViewActivity.this);
        }

        @JavascriptInterface
        public void toChongZhi() {
            AppRouter.routeToRecharge(getApplicationContext());
        }
    }
}

